package com.bkgy.modular.rawMaterialRules.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bkgy.modular.rawMaterialRules.entity.LThight;
import com.bkgy.modular.rawMaterialRules.mapper.LThightMapper;
import com.bkgy.modular.rawMaterialRules.service.LThightService;
import com.bkgy.sys.common.entity.ResultData;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ZhuHongKai
 * @version 1.0
 * @ClassName LThightServiceImpl
 * @date 2023/1/4 18:09
 */
@Service
public class LThightServiceImpl extends ServiceImpl<LThightMapper, LThight> implements LThightService {
    @Resource
    private LThightMapper lThightMapper;

    /**
     * 2130酸轧成品连退原料厚度阈值数据查询
     * @return
     */
    @Override
    public ResultData findLThightData() {
        List<LThight> list = lThightMapper.selectList(null);
        return ResultData.ResultDataFaultSelf("操作成功",list);
    }

    /**
     * 2130酸轧成品连退原料厚度阈值数据更新
     * @param jsonObject
     * @return
     */
    @Override
    public ResultData updateLThightData(JSONObject jsonObject) {
        try {
            // 获取前端传进的数据
            String dataId = jsonObject.getString("dataId");

            // 构建查询条件
            LambdaQueryWrapper<LThight> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(LThight::getGrade1,dataId);

            // 获取传进需要修改的数据,并转成String类型
            String data = jsonObject.getJSONObject("data").toJSONString();

            // 调用json中的parseObject,将字符串data转成java对象
            LThight LThight = JSON.parseObject(data, LThight.class);

            // 调用mapper进行修改
            lThightMapper.update(LThight,queryWrapper);
            return ResultData.ResultDataFaultSelf("更新成功",null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("修改失败，错误信息为" + e.getMessage(), null);
        }
    }
}
